// pages/micro_shop_share_category/micro_shop_share_category.js
var app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {
    isSelect: '',
    shop_id: '',
    state: {
      adv_img: '',
      adv_url: ''
    },
    list: [],
    contentlist: {},
    shoplang:''
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    if (options.isSelect) {
      this.setData({
        isSelect: options.isSelect
      });
    }
    if (options.shop_id) {
      this.setData({
        shop_id: options.shop_id
      });
    }
    this.getList();
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function(res) {
      // 来自右上角转发按钮
      var value = wx.getStorageSync('yz_uid');
      var mid = '';
      if (value) {
        mid = value;
      }
      return {
        path: '/packageC/microshop/micro_shop_share_category/micro_shop_share_category?shop_id=' + this.options.shop_id + '&mid=' + mid
      };
  },
  gomicroShopCategory() {
    //micro_shop_category
  },
  getList() {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.CategoryByShare.getCategory');
    urlStr += "&shop_id=" + that.data.shop_id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          // var myData = res.data;
          that.setData({
            list: res.data.data
          });
          if (res.data.lang) {
            that.setData({
              shoplang: res.data.lang
            });
            wx.setNavigationBarTitle({
              title: that.data.shoplang + '分类'
            });
          }
          if (app._isTextEmpty(res.data) || app._isTextEmpty(res.data.data) || app._isTextEmpty(
            res.data.data[0])) {
            return;
          }
          that.tabsaData(0, res.data.data[0]); //默认获取第一个子分类
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  tabsaData(n, i) {
    this.setData({
      'state.adv_img': i.adv_img,
      'state.adv_url': i.adv_url
    });
    this.setData({
      active: n,
      active2: false
    });
    this.getContentlist(i.id);
  },
  getContentlist(id) {
    let that = this;
    let urlStr = app.getNetAddresss('plugin.micro.frontend.controllers.MicroShop.CategoryByShare.getChildrenCategory');
    urlStr += "&parent_id=" + id;
    urlStr += "&shop_id=" + that.data.shop_id;
    app._getNetWork({
      url: urlStr,
      success: function(resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let contentlist = {
            data: res.data.data,
            tjfl: false,
            set: res.data.set
          };
          that.setData({
            err: false,
            contentlist,
          });
        } else {
          that.setData({
            err: true
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  tabsa(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.tabsaData(index, item);
  }
});
